const ProcurementPlanModel = require('../model/ProcurementPlanModel');

class ProcurementPlanController {
    constructor() {
        this.procurementPlanModel = new ProcurementPlanModel();
    }

    /**
     * 获取所有采购计划（支持筛选和分页）
     */
    async getAllPlans(req, res) {
        try {
            const result = await this.procurementPlanModel.getAllPlans(req.query);
            res.json({
                msg: '采购计划列表获取成功',
                code: 0,
                data: result.list,
                pagination: result.pagination
            });
        } catch (error) {
            res.status(500).json({
                msg: '获取采购计划列表失败',
                code: 1
            });
        }
    }

    /**
     * 根据ID获取采购计划详情
     */
    async getPlanById(req, res) {
        try {
            const planId = req.params.id;
            const plan = await this.procurementPlanModel.getPlanById(planId);

            if (!plan) {
                return res.status(404).json({ msg: '采购计划不存在' });
            }
            res.json({ msg: "获取采购计划详情成功", code: 0, data: plan });
        } catch (error) {
            res.status(500).json({
                msg: error.message,
                code: 1
            });
        }
    }

    /**
     * 创建新的采购计划
     */
    async createPlan(req, res) {
        try {
            const planData = req.body;
            console.log("请求参数：", planData)

            // 基础校验
            if (!planData.planDate || !planData.createdUid) {
                return res.status(400).json({
                    code: 1,
                    msg: '计划日期和创建人不能为空'
                });
            }

            const result = await this.procurementPlanModel.createPlan(planData);
            res.status(200).json({

                msg: '采购计划创建成功',
                code: 0,
                data: { planId: result.planId }
            });
        } catch (error) {
            res.status(500).json({

                msg: error.message,
                code: 1
            });
        }
    }

    /**
     * 更新采购计划
     */
    async updatePlan(req, res) {
        try {
            const planId = req.params.id;
            const planData = req.body;

            const result = await this.procurementPlanModel.updatePlan(planId, planData);
            res.json({
                msg: '采购计划更新成功',
                code: 0,
                data: result
            });
        } catch (error) {
            res.status(500).json({
                msg: error.message,
                code: 1
            });
        }
    }

    /**
     * 删除采购计划
     */
    async deletePlan(req, res) {
        try {
            const planId = req.params.id;

            // 校验计划是否存在
            const existingPlan = await this.procurementPlanModel.getPlanById(planId);
            if (!existingPlan) {
                return res.status(404).json({
                    msg: '采购计划不存在',
                    code: 1
                });
            }

            // 校验计划状态（已审批的计划不允许删除）
            if (existingPlan.status === 'approved') {
                return res.status(400).json({
                    msg: '已审批的采购计划不允许删除',
                    code: 1
                });
            }

            const result = await this.procurementPlanModel.deletePlan(planId);
            res.json({
                msg: '采购计划删除成功',
                data: result
            });
        } catch (error) {
            res.status(500).json({ msg: error.message, code: 1 });
        }
    }

    /**
     * 更新采购计划状态
     */
    async updatePlanStatus(req, res) {
        try {
            const planId = req.params.id;
            const { status, operatorId } = req.body;

            if (!status || !operatorId) {
                return res.status(400).json({
                    msg: '状态和操作人不能为空',
                    code: 1
                });
            }
            const result = await this.procurementPlanModel.updatePlanStatus(planId, status, operatorId);
            res.json({
                msg: '采购计划状态更新成功',
                data: result,
                code: 0
            });
        } catch (error) {
            res.status(500).json({ msg: error.message, code: 1 });
        }
    }
}

module.exports = ProcurementPlanController;